home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / text / hyper / ppguide.lha / ppguide.c < prev    next >
C/C++ Source or Header  |  1993-05-14  |  6KB  |  186 lines

  1. /* ppguide.c -- views PowerPacked .guide files */
  2. /* Copyright ©1993 William E. Sorensen.  All rights reserved. */
  3.  
  4. #define ONESECOND 50UL
  5. #define WINDEF "CON:0/0/640/100/ppguide"
  6.  
  7. #include <functions.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/amigaguide.h>
  13. #include <clib/amigaguide_protos.h>
  14. #include <pragmas/amigaguide_pragmas.h>
  15. #include <libraries/powerpacker.h>
  16. #include <libraries/ppbase.h>
  17. #include <workbench/startup.h>
  18.  
  19. VOID getprogarg(int,char **);
  20. VOID cleanexit(LONG);
  21.  
  22. struct PPBase *PPBase=NULL;
  23. struct Library *AmigaGuideBase=NULL;
  24. struct NewAmigaGuide nag={NULL};
  25.  
  26. char *progarg=NULL;  /* will point to a copy of the program's argument */
  27. ULONG pperror,len;
  28. UBYTE *buffer=NULL;
  29. BPTR tempfile=NULL;
  30. BOOL runfromCLI,tempfileexists=FALSE;
  31. AMIGAGUIDECONTEXT aghandle=NULL;
  32.  
  33. char tempfilename[]="T:ppguide.tmp";  /* hopefully, this is in RAM: */
  34. char topenerr[]="Unable to open temporary file";
  35. char twriteerr[]="Unable to write to temporary file";
  36. char tshowerr[]="Unable to show temporary file";
  37.  
  38. char VersionString[]="$VER: ppguide 1.0 (14.5.93)";
  39.  
  40. char usagestr[]="Usage:  ppguide <file>\n";
  41.  
  42. extern struct Library *DOSBase;
  43. extern struct WBStartup *WBenchMsg;  /* SAS users may have to use argv */
  44. extern long Enable_Abort;  /* Manx (for Ctrl-C control) */
  45.  
  46. VOID main(int argc, char *argv[])
  47. {
  48.     Enable_Abort=0L;  /* turn off Manx's Ctrl-C handling */
  49.  
  50.     getprogarg(argc,argv);  /* get the program's argument */
  51.  
  52.     /* if it was a question mark, print an informational message */
  53.     if(!strcmp(progarg,"?")){
  54.         printf("%s\nCopyright ©1993 William E. Sorensen.  All rights "
  55.                "reserved.\n%s",VersionString+6,usagestr);
  56.         cleanexit(RETURN_OK);
  57.     }
  58.  
  59.     if (!(AmigaGuideBase=OpenLibrary((UBYTE *)"amigaguide.library",33L))){
  60.         printf("You need amigaguide.library V33+!\n");
  61.         cleanexit(RETURN_FAIL);
  62.     }
  63.  
  64.     if(!(PPBase=(struct PPBase *)OpenLibrary((UBYTE *)"powerpacker.library",33L))){
  65.         printf("You need powerpacker.library V33+!\n");
  66.         cleanexit(RETURN_FAIL);
  67.     }
  68.  
  69.     if(pperror=ppLoadData(progarg,DECR_POINTER,MEMF_ANY,&buffer,&len,NULL)){
  70.         printf("Load error: %s\n",ppErrorMessage(pperror));
  71.         cleanexit(RETURN_ERROR);
  72.     }
  73.  
  74.     if(!(tempfile=Open((UBYTE *)tempfilename,MODE_NEWFILE))){
  75.         /* PrintFault() requires V36+ and writes to stderr, not stdout */
  76.         if(((DOSBase->lib_Version)>=36) && runfromCLI)
  77.             PrintFault(IoErr(),(UBYTE *)topenerr);
  78.         else
  79.             printf("%s\n",topenerr);
  80.         cleanexit(RETURN_ERROR);
  81.     }
  82.     tempfileexists=TRUE;
  83.     if(len!=Write(tempfile,buffer,len)){
  84.         if(((DOSBase->lib_Version)>=36) && runfromCLI)
  85.             PrintFault(IoErr(),(UBYTE *)twriteerr);
  86.         else
  87.             printf("%s\n",twriteerr);
  88.         Close(tempfile);
  89.         cleanexit(RETURN_ERROR);
  90.     }
  91.     Close(tempfile);  /* we have to do this now, not in cleanexit() */
  92.  
  93.     if(buffer){
  94.         /* get some memory back (we might need it!) */
  95.         FreeMem(buffer,len);
  96.         buffer=NULL;  /* so we don't free it twice */
  97.     }
  98.  
  99.     nag.nag_Name=(STRPTR)tempfilename;
  100.  
  101.     /* BaseName is barely documented, but is REQUIRED to avoid an
  102.        Enforcer hit!  (Looks like YOUR bug, CBM!) */
  103.     nag.nag_BaseName=(STRPTR)"ppguide";
  104.  
  105.     /* OpenAmigaGuide() will not return until user closes all windows. */
  106.     if (!(aghandle=OpenAmigaGuide(&nag,NULL))){
  107.         if(((DOSBase->lib_Version)>=36) && runfromCLI)
  108.             PrintFault(IoErr(),(UBYTE *)tshowerr);
  109.         else
  110.             printf("%s\n",tshowerr);
  111.         cleanexit(RETURN_ERROR);
  112.     }
  113.     CloseAmigaGuide(aghandle);
  114.  
  115.     cleanexit(RETURN_OK);
  116. }
  117.  
  118. VOID getprogarg(int mainargc,char **mainargv)
  119. /* parameters should be argc and argv from main() */
  120. /* sets up output for Workbench or CLI; sets global variable
  121.    progarg to point to program's single argument (and exits with
  122.    an appropriate error message if the number of arguments is !=1) */
  123. {
  124.     struct WBArg *wbarg;
  125.     char *tempargptr;
  126.  
  127.     if(mainargc==0){
  128.         /* program was started from Workbench */
  129.         /* open a CON window for output (and change stdout to this) */
  130.         if(!freopen(WINDEF,"w+",stdout)){
  131.             runfromCLI=TRUE;  /* so cleanexit doesn't wait */
  132.             cleanexit(RETURN_ERROR);
  133.         }
  134.         runfromCLI=FALSE;
  135.         if(WBenchMsg->sm_NumArgs!=2L){
  136.             printf("You must select exactly one project icon!\n");
  137.             cleanexit(RETURN_WARN);
  138.         }
  139.         wbarg=WBenchMsg->sm_ArgList+1;  /* 2nd argument is name of project */
  140.         /* CD to wbarg's directory, if there is one */
  141.         if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  142.             CurrentDir(wbarg->wa_Lock);
  143.         tempargptr=wbarg->wa_Name;
  144.     }
  145.     else{
  146.         /* program was started from CLI */
  147.         runfromCLI=TRUE;
  148.         if(mainargc!=2){
  149.             printf("%s",usagestr);
  150.             cleanexit(RETURN_WARN);
  151.         }
  152.         tempargptr=mainargv[1];
  153.     }
  154.  
  155.     /* allocate space for the argument, then copy it to there */
  156.     if(!(progarg=(char *)malloc(strlen(tempargptr)+1))){
  157.         printf("Unable to allocate memory!\n");
  158.         cleanexit(RETURN_ERROR);
  159.     }
  160.     strcpy(progarg,tempargptr);
  161. }
  162.  
  163. VOID cleanexit(LONG rc)
  164. {
  165.     if(tempfileexists)
  166.         DeleteFile((UBYTE *)tempfilename);  /* don't care if it fails */
  167.  
  168.     /* ppLoadData alloc'd mem; we don't FreeMem unless ppLoadData worked */
  169.     if((!pperror) && buffer)
  170.         FreeMem(buffer,len);
  171.  
  172.     if(PPBase)
  173.         CloseLibrary((struct Library *)PPBase);
  174.  
  175.     if(AmigaGuideBase)
  176.         CloseLibrary(AmigaGuideBase);
  177.  
  178.     if(progarg)
  179.         free((void *)progarg);  /* deallocate memory for string */
  180.  
  181.     if(!runfromCLI)
  182.         Delay(ONESECOND);  /* when exit() closes stdout, window closes */
  183.  
  184.     exit(rc);
  185. }
  186.